/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package component;

import ddf.minim.AudioPlayer;
import processing.core.PApplet;

/**
 *
 * @author biron
 */
public class ButtonRewind extends AButton
{
  boolean               _invert;
  boolean               _pressed;
  private AudioPlayer   _player;
  
  public ButtonRewind(int x, int y, int hw, int hh, PApplet app, AudioPlayer player)
  {
    super(x, y, hw, hh, app);
    _invert = false;
    _player = player;
  }
  
  // code used to scrub backward in the file
  public void update()
  {
    // if the rewind button is currently being _pressed
    if (_pressed)
    {
      // get the current song position
      int pos = _player.position();
      // if it greater than 200 milliseconds
      if ( pos > 200 )
      {
        // rewind the song by 200 milliseconds
        _player.skip(-200);
      }
      else
      {
        // if the song hasn't played more than 100 milliseconds
        // just rewind to the beginning
        _player.rewind();
      }
    }
  }
  
  public void mousePressed()
  {
    _pressed = pressed();
    if ( _pressed ) 
    {
      _invert = true;
      // if the song isn't currently playing, rewind it to the beginning
      if ( !_player.isPlaying() ) _player.rewind();      
    }
  }
  
  public void mouseReleased()
  {
    _pressed = false;
    _invert = false;
  }

  public void draw()
  {
    if ( _invert )
    {
      _app.fill(255);
      _app.stroke(0);
    }
    else
    {
      _app.noFill();
      _app.stroke(255);
    }
    
    _app.rect(x - hw, y - hh, hw*2, hh*2);
    if ( _invert )
    {
      _app.fill(0);
      _app.stroke(255);
    }
    else
    {
      _app.fill(255);
      _app.noStroke();
    }
    
    _app.triangle(x - hw/2, y, x, y - hh/2, x, y + hh/2);
    _app.triangle(x, y, x + hw/2, y - hh/2, x + hw/2, y + hh/2);    
  }  

    /**
     * @param _player the _player to set
     */
    public void setPlayer(AudioPlayer _player) {
        this._player = _player;
    }
}